//
//  DateUtils.m
//  Guazi
//
//  Created by koolearn on 11-7-31.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DateUtils.h"


@implementation DateUtils

+ (NSString*)diffNowString:(time_t)sinceTime
{
    // Calculate distance time string
    //
    time_t now;
    time(&now);
    
    int distance = (int)difftime(now, sinceTime);
    if (distance < 0) distance = 0;
    
    NSString* timestamp; 
    if (distance < 60) {
        timestamp = [NSString stringWithFormat:@"%d%@", distance, @"秒钟前"];
    }
    else if (distance < 60 * 60) {  
        distance = distance / 60;
        timestamp = [NSString stringWithFormat:@"%d%@", distance, @"分钟前"];
    }  
    else if (distance < 60 * 60 * 24) {
        distance = distance / 60 / 60;
        timestamp = [NSString stringWithFormat:@"%d%@", distance, @"小时前"];
    }
    else if (distance < 60 * 60 * 24 * 7) {
        distance = distance / 60 / 60 / 24;
        timestamp = [NSString stringWithFormat:@"%d%@", distance, @"天前"];
    }
    else if (distance < 60 * 60 * 24 * 7 * 4) {
        distance = distance / 60 / 60 / 24 / 7;
        timestamp = [NSString stringWithFormat:@"%d%@", distance, @"周前"];
    }
    else {
        static NSDateFormatter *dateFormatter = nil;
        if (dateFormatter == nil) {
            dateFormatter = [[NSDateFormatter alloc] init];
			[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
            //[dateFormatter setDateStyle:NSDateFormatterShortStyle];
            //[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
        }
        
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:sinceTime];        
        timestamp = [dateFormatter stringFromDate:date];
    }
    return timestamp;
}

@end
